#!/bin/bash

. $TEST_ROOT_DIR/run-test-common

  # This test uses a large dataset which is not part of the CNTK repository itself
  # We use the dataset from an external location specified using an environment variable
  if [[ "$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY" == "" || ! -d "$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY" ]]; then
    echo 'This test uses external data that is not part of the CNTK repository. Environment variable CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY must be set to point to the external test data location'
    exit 1
  fi

  TestDataDir=$TEST_BIN_DIR/TestDataExamples
  mkdir -p $TestDataDir

  # Copy and extract animal data to the test run directory
  if [ "$OS" == "Windows_NT" ]; then
    DataSourceDir=`cygpath -au $CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY`/Image
  else
    DataSourceDir=$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY/Image
  fi
  echo cp -R $DataSourceDir/Animals $TestDataDir
  cp -R $DataSourceDir/Animals $TestDataDir || exit $?

  # Copy resnet model to the test run directory
  if [ "$OS" == "Windows_NT" ]; then
    ModelSource=`cygpath -au $CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY`/PreTrainedModels/ResNet/v1/ResNet_18.model
  else
    ModelSource=$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY/PreTrainedModels/ResNet/v1/ResNet_18.model
  fi
  cp $ModelSource $TestDataDir || exit $?

  if [ "$OS" == "Windows_NT" ]; then      
    TestDataDirForPlatform=`cygpath -aw $TestDataDir`
  else
    TestDataDirForPlatform=$TestDataDir
  fi
  
  echo Extracting animal data to  $TestDataDirForPlatform
  python extract_animal_data.py $TestDataDirForPlatform/Animals/Animals.zip $TestDataDirForPlatform

  pushd $TestDataDir
  if [ "$OS" == "Windows_NT" ]; then      
    pushd $TEST_BIN_DIR > /dev/null
  else
    pushd $TEST_BIN_DIR/../lib > /dev/null
  fi

  dotnet CNTKLibraryCSTrainingTest.dll TransferLearningTest $TestDataDirForPlatform
  popd > /dev/null
  # Delete the test data
  popd
  rm -rf $TestDataDir
	
  exit 0